#if os(macOS)
import AppKit

open class UVisualEffectView: NSVisualEffectView, AnyDeclarativeProtocol, DeclarativeProtocolInternal {
    public var declarativeView: UVisualEffectView { self }
    public lazy var properties = Properties<UVisualEffectView>()
    lazy var _properties = PropertiesInternal()
    
    @UState public var height: CGFloat = 0
    @UState public var width: CGFloat = 0
    @UState public var top: CGFloat = 0
    @UState public var leading: CGFloat = 0
    @UState public var left: CGFloat = 0
    @UState public var trailing: CGFloat = 0
    @UState public var right: CGFloat = 0
    @UState public var bottom: CGFloat = 0
    @UState public var centerX: CGFloat = 0
    @UState public var centerY: CGFloat = 0
    
    var __height: UState<CGFloat> { _height }
    var __width: UState<CGFloat> { _width }
    var __top: UState<CGFloat> { _top }
    var __leading: UState<CGFloat> { _leading }
    var __left: UState<CGFloat> { _left }
    var __trailing: UState<CGFloat> { _trailing }
    var __right: UState<CGFloat> { _right }
    var __bottom: UState<CGFloat> { _bottom }
    var __centerX: UState<CGFloat> { _centerX }
    var __centerY: UState<CGFloat> { _centerY }
    
    private lazy var  _tag: Int = -1
    public override var tag: Int {
        get { _tag }
        set { _tag = newValue }
    }
    
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        _setup()
    }
    
    public init () {
        super.init(frame: .zero)
        _setup()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func _setup() {
        translatesAutoresizingMaskIntoConstraints = false
        wantsLayer = true
    }
    
    /// A value indicating which material is shown by the NSVisualEffectView.
    /// See the comments on NSVisualEffectMaterial.
    /// Defaults to NSVisualEffectMaterialAppearanceBased.
    /// You should instead specify an appropriate semantic material value.  See the comments on NSVisualEffectMaterial.
    @discardableResult
    public func material(_ v: NSVisualEffectView.Material) -> Self {
        material = v
        return self
    }

    /// A value controlling how the NSVisualEffectView generates its material.
    /// See the comments on NSVisualEffectBlendingMode.
    /// Not all materials support both blending modes, so NSVisualEffectView may fall back to a more appropriate blending mode as needed.
    /// Defaults to NSVisualEffectBlendingModeBehindWindow.
    @discardableResult
    public func blendingMode(_ v: NSVisualEffectView.BlendingMode) -> Self {
        blendingMode = v
        return self
    }

    /// A value controlling when the NSVisualEffectView takes on the active look.
    /// See the comments on NSVisualEffectState.
    /// Defaults to NSVisualEffectStateFollowsWindowActiveState.
    @discardableResult
    public func state(_ v: NSVisualEffectView.State) -> Self {
        state = v
        return self
    }
    
    /// An image whose alpha channel is used to mask the material generated by the NSVisualEffectView.
    /// (It does not also mask subviews.)
    /// Defaults to nil.
    /// It is best to set this to the smallest mask image possible and properly set the image's capInsets property to stretch it.
    /// Setting the maskImage on an NSVisualEffectView that is the contentView of a window will correctly influence the window's shadow.
    @discardableResult
    public func maskImage(_ v: NSImage?) -> Self {
        maskImage = v
        return self
    }
    
    /// When YES, the material takes on the emphasized look.  Defaults to NO.  Some, but not all, materials change their look when emphasized.  This is used to indicate that an associated view has firstResponder status.
    @discardableResult
    public func emphasized(_ v: Bool = true) -> Self {
        isEmphasized = v
        return self
    }
    
//    var _defaultEffect, _darkEffect: UIVisualEffect?
    
//    public override init(effect: UIVisualEffect?) {
//        self._defaultEffect = effect
//        super.init(effect: effect)
//        switchEffect()
//        translatesAutoresizingMaskIntoConstraints = false
//    }
//
//    public init(_ effect: UIVisualEffect?, _ darkEffect: UIVisualEffect? = nil) {
//        self._defaultEffect = effect
//        self._darkEffect = darkEffect
//        super.init(effect: effect)
//        switchEffect()
//        translatesAutoresizingMaskIntoConstraints = false
//    }
//
//    public init () {
//        super.init(effect: nil)
//        switchEffect()
//        translatesAutoresizingMaskIntoConstraints = false
//    }
    
    open override func layout() {
        super.layout()
        onLayoutSubviews()
    }
    
    open override func viewDidMoveToSuperview() {
        super.viewDidMoveToSuperview()
        movedToSuperview()
    }

    open override func viewWillMove(toWindow newWindow: NSWindow?) {
        super.viewWillMove(toWindow: newWindow)
    }
    
//    @discardableResult
//    public func effect(_ effect: NSVisualEffect?) -> Self {
//        self._defaultEffect = effect
//        switchEffect()
//        return self
//    }
//
//    @discardableResult
//    public func darkEffect(_ effect: NSVisualEffect?) -> Self {
//        self._darkEffect = effect
//        switchEffect()
//        return self
//    }
//
//    open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
//        switchEffect()
//    }
//
//    var isDark: Bool {
//        if #available(iOS 12.0, *) {
//            return traitCollection.userInterfaceStyle == .dark
//        }
//        return false
//    }
//
//    private func switchEffect() {
//        if isDark, let de = _darkEffect {
//            effect = de
//            return
//        }
//        effect = _defaultEffect
//    }
}

// MARK: Convenience Initializers

//extension UIVisualEffect {
//    static func effect(_ effect: UIVisualEffect?) -> VisualEffectView {
//        VisualEffectView(effect)
//    }
//}
#endif
